package com.jie.rabbitmq.config;
 
//import com.sun.org.apache.regexp.internal.RE;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.jie.constants.RabbitMQConstant.*;

/**
 * @ClassName RabbitmqConfig
 * @Description
 * @Author 胡泽
 * @Date 2019/12/17 12:35
 * @Version 1.0
 */
@Configuration
public class RabbitmqConfig {
    /**
     * 消息转换器
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(messageConverter());
        return template;
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }


    //配置邮箱的消息队列
    @Bean
    Queue directSignCodeQueue(){
        return new Queue(SIGN_CODE_QUEUE);
    }
    @Bean
    public Queue directSystemEmailQueue(){
        return new Queue(SYSTEM_EMAIL_QUEUE);
    }
    //配置文章的队列
    @Bean
    public Queue directArticlePublicQueue(){
        return new Queue(ARTICLE_PUBLISH_QUEUE);
    }
    @Bean
    public Queue directNoticeQueue(){
        return new Queue(NOTICE_QUEUE);
    }
    @Bean
    public  DirectExchange directNoticeExchange(){
        return new DirectExchange(NOTICE_EXCHANGE,true,false);
    }
    @Bean
    public DirectExchange directArticlePublicExchange(){
        return new DirectExchange(ARTICLE_PUBLISH_EXCHANGE,true,false);
    }
    //配置邮箱验证码交换机
    @Bean
    DirectExchange directExchange(){
        //参数1：交换机的名称  参数2：队列重启后是否删除  参数3：队列长期不用是否自动删除
        return new DirectExchange(EMAIL_CODE_EXCHANGE,true,false);
    }
    //配置系统邮箱交换机，开手动ack+手动补偿 做的全局化
    @Bean
    TopicExchange directSystemEmailExchange(){
        return new TopicExchange(SYSTEM_EMAIL_EXCHANGE,true,false);
    }
    //将消息队列和交换机绑定
    @Bean
    public Binding directQueueBindingSignCode(){
        return BindingBuilder
                .bind(directSignCodeQueue())
                .to(directExchange())
                .with(EMAIL_CODE_KEY);
    }
    /**
     * 绑定系统公告信息交换机
     */
    @Bean
    public  Binding bindingNoticePublic() {
        return BindingBuilder.bind(directNoticeQueue())
                .to(directNoticeExchange())
                .with(NOTICE_KEY);
    }
    /**
     * 绑定全体邮箱信息交换机
     */
    @Bean
    public Binding articleQueueBinding() {
        return BindingBuilder.bind(directSystemEmailQueue())
                .to(directSystemEmailExchange())
                .with(EMAIL_SYSTEM_KEY);
    }
    @Bean
    public Binding bindingArticlePublic() {
        return  BindingBuilder.bind(directArticlePublicQueue())
                .to(directArticlePublicExchange())
                .with(ARTICLE_PUBLISH_KEY);
    }

 
}